package com.stone.business.jc.action;

/**
 * <pre>
 * 工作质量考核
 * </pre>
 */

import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.jc.pojo.TGzzlkh;
import com.stone.business.jc.service.JcCommonService;
import com.stone.utils.JsonUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.page.CustomPage;

public class GzzlkhAction extends BaseListAction {

	private static final long serialVersionUID = 3979577965804736350L;
	
	private String start;
	private String limit;
	
	private String userid;
	
	private String id;

	private JcCommonService myService;
	
	private TGzzlkh entity;

	public void setMyService(JcCommonService myService) {
		this.service = myService;
		this.myService = myService;
	}
	
    /** * 构造方法. */
    public GzzlkhAction() {
        this.entityClass = TGzzlkh.class;
    }
    
    /**
     * 初始化进入
     */
    public String initDetail() throws Exception {
    	
    	// 考勤日期 默认上个月
    	List<HashMap<String,Object>> list = myService.findListBySql("SELECT TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AS NOWMONTH  FROM DUAL");
    	
		super.getSession().setAttribute("KQRQ", list.get(0).get("nowmonth"));
		
		return SUCCESS;
    }
    
    /**
     * 保存
     * @throws Exception
     */
    public void save() throws Exception {
    	
    	if (entity.getId() == 0) {
    		myService.save(entity);
    	} else {
    		myService.update(entity);
    	}
    	
        super.getResponse().getWriter().print("{msg:'保存成功'}");
    	
    }
    
    /**
     * 删除
     * @throws Exception
     */
    public void delete() throws Exception {
    	
    	myService.delete(Long.parseLong(id), "TGzzlkh");
    	
        super.getResponse().getWriter().print("{msg:'删除成功'}");
    	
    }
    
	/**
	 * 用户的
	 * 
	 * @return
	 * @throws AppException
	 */
	public void getListExt() throws Exception {

		super.setPage((String.valueOf((Integer.parseInt(getStart()) / Integer.parseInt(getLimit()) + 1))));
		
		HashMap<String, Object> m = new HashMap<String, Object>();
		
		String cols = " kq.id as id, kq.userid as userid,kq.khrq as khrq, kq.khxzid as khxzid, kq.khxzremark as khxzremark, kq.fs as fs";

		m.put("cols", cols);
		m.put("alias", "kq");
		m.put("join", "");
	
		String conditions = " kq.userid = '" + userid + "'";// '"+super.getSearchBean().getCon1()+"'");

		m.put("conditions",conditions);//conditions);
		m.put("clause", " order by kq.khrq desc");
		
		// 根据起始记录查询当前页记录
		List<HashMap<String,Object>> list = myService.getPageListByDHql(Integer.parseInt(getStart()), Integer.parseInt(getLimit()) , m,this.entityClass.getName() );
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM");
    	
        JSON json = JSONSerializer.toJSON(list, jsonConfig);

		String strTree = "{result:" + json.toString() + ",totalCount:[" + service.getTotalByDHql(conditions, null, this.entityClass.getName() + " as kq"  ) + "]}";
		
        super.getResponse().getWriter().print(strTree);
		
	}

	public String getStart() {
		return start == null ? "0" : start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getLimit() {
		return limit == null ? String.valueOf(CustomPage.PER_PAGE) : limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	@Override
	public HashMap<String, Object> setSearchCondition() {
		// TODO Auto-generated method stub
		return null;
	}

	public TGzzlkh getEntity() {
		return entity;
	}

	public void setEntity(TGzzlkh entity) {
		this.entity = entity;
	}

	public void setId(String id) {
		this.id = id;
	}

	
	
}
